﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MxWeiXinPF.Common;
using MxWeiXinPF.DBUtility;

namespace MxWeiXinPF.DAL
{
    public partial class wx_userinfo_BaseData
    {
        public int Add(MxWeiXinPF.Model.wx_userinfo_BaseData model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into wx_Userinfo(");
            builder.Append("[wid],[gid],[openid],[subscribe],[nickname],[sex],[language],[city],[province],[country],[headimgul],[subscribe_time],[unsubscribe_time],[remark],[userid],[groupid],[groupname],[unionid],[parentopenid],[classname],[subopenid],[extInt],[extStr1],[extStr2],[extStr3])");
            builder.Append(" values (");
            builder.Append("@wid,@gid,@openid,@subscribe,@nickname,@sex,@language,@city,@province,@country,@headimgul,@subscribe_time,@unsubscribe_time,@remark,@userid,@groupid,@groupname,@unionid,@parentopenid,@classname,@subopenid,@extInt,@extStr1,@extStr2,@extStr3])");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@gid", SqlDbType.VarChar, 100), new SqlParameter("@openid", SqlDbType.VarChar, 100), new SqlParameter("@subscribe", SqlDbType.Int, 4), new SqlParameter("@nickname", SqlDbType.VarChar, 50), new SqlParameter("@sex", SqlDbType.Int, 4), new SqlParameter("@language", SqlDbType.VarChar, 50), new SqlParameter("@city", SqlDbType.NVarChar, 20), new SqlParameter("@province", SqlDbType.NVarChar, 20), new SqlParameter("@country", SqlDbType.NVarChar, 50), new SqlParameter("@headimgul", SqlDbType.VarChar, 500), new SqlParameter("@subscribe_time", SqlDbType.DateTime), new SqlParameter("@unsubscribe_time", SqlDbType.DateTime), new SqlParameter("@remark", SqlDbType.VarChar, 500), new SqlParameter("@userid", SqlDbType.Int, 4), new SqlParameter("@groupid", SqlDbType.Int, 4), 
                new SqlParameter("@groupname", SqlDbType.NVarChar, 100), new SqlParameter("@unionid", SqlDbType.VarChar, 100), new SqlParameter("@parentopenid", SqlDbType.VarChar, 100), new SqlParameter("@classname", SqlDbType.VarChar, 50), new SqlParameter("@subopenid", SqlDbType.VarChar, 500), new SqlParameter("@extInt", SqlDbType.Int, 4), new SqlParameter("@extStr1", SqlDbType.VarChar, 500), new SqlParameter("@extStr2", SqlDbType.VarChar, 100), new SqlParameter("@extStr3", SqlDbType.VarChar, 100)
             };
            cmdParms[0].Value = model.wid;
            cmdParms[1].Value = model.gid;
            cmdParms[2].Value = model.openid;
            cmdParms[3].Value = model.subscribe;
            cmdParms[4].Value = model.nickname;
            cmdParms[5].Value = model.sex;
            cmdParms[6].Value = model.language;
            cmdParms[7].Value = model.city;
            cmdParms[8].Value = model.province;
            cmdParms[9].Value = model.country;
            cmdParms[10].Value = model.headimgul;
            cmdParms[11].Value = model.subscribe_time;
            cmdParms[12].Value = model.unsubscribe_time;
            cmdParms[13].Value = model.remark;
            cmdParms[14].Value = model.userid;
            cmdParms[15].Value = model.groupid;
            cmdParms[0x10].Value = model.groupname;
            cmdParms[0x11].Value = model.unionid;
            cmdParms[0x12].Value = model.parentopenid;
            cmdParms[0x13].Value = model.subopenid;
            cmdParms[20].Value = model.extInt;
            cmdParms[0x15].Value = model.extStr2;
            cmdParms[0x16].Value = model.extStr3;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public MxWeiXinPF.Model.wx_userinfo_BaseData DataRowToModel(DataRow row)
        {
            MxWeiXinPF.Model.wx_userinfo_BaseData data = new MxWeiXinPF.Model.wx_userinfo_BaseData();
            if (row != null)
            {
            }
            return data;
        }

        public bool Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from wx_Userinfo ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string idlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from wx_Userinfo ");
            builder.Append(" where id in (" + idlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from wx_Userinfo");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public Model.wx_user_groups GetDefault()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select top 1 id from wx_UserGroup ");
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single != null)
            {
                return this.GetModelUserGroup(Convert.ToInt32(single));
            }
            return null;
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select id,wid,wx_openid,requestType,requestContent,responseType,reponseContent,createTime,wx_xmlContent,createDate,flag,rType,remark,extInt,extStr,extStr2,extStr3 ");
            builder.Append(" FROM wx_Userinfo ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" id,wid,wx_openid,requestType,requestContent,responseType,reponseContent,createTime,wx_xmlContent,createDate,flag,rType,remark,extInt,extStr,extStr2,extStr3 ");
            builder.Append(" FROM wx_Userinfo ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from  wx_Userinfo ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where  " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(builder.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, builder.ToString(), filedOrder));
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.id desc");
            }
            builder.Append(")AS Row, T.*  from wx_Userinfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "wx_Userinfo");
        }

        public MxWeiXinPF.Model.wx_userinfo_BaseData GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 [wid],[gid],[openid],[subscribe],[nickname],[sex],[language],[city],[province],[country],[headimgul],[subscribe_time],[unsubscribe_time],[remark],[userid],[groupid],[groupname],[unionid],[parentopenid],[classname],[subopenid],[extInt],[extStr1],[extStr2],[extStr3] from wx_Userinfo ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            MxWeiXinPF.Model.wx_userinfo_BaseData data = new MxWeiXinPF.Model.wx_userinfo_BaseData();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public Model.wx_user_groups GetModelUserGroup(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 [id],[wid],[groupname],[classname],[gid],[exStr] from wx_UserGroup ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            Model.wx_user_groups _groups = new Model.wx_user_groups();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                if ((set.Tables[0].Rows[0]["id"] != null) && (set.Tables[0].Rows[0]["id"].ToString() != ""))
                {
                    _groups.id = int.Parse(set.Tables[0].Rows[0]["id"].ToString());
                }
                if ((set.Tables[0].Rows[0]["groupname"] != null) && (set.Tables[0].Rows[0]["groupname"].ToString() != ""))
                {
                    _groups.groupname = set.Tables[0].Rows[0]["groupname"].ToString();
                }
                if ((set.Tables[0].Rows[0]["classname"] != null) && (set.Tables[0].Rows[0]["classname"].ToString() != ""))
                {
                    _groups.classname = set.Tables[0].Rows[0]["classname"].ToString();
                }
                if ((set.Tables[0].Rows[0]["gid"] != null) && (set.Tables[0].Rows[0]["gid"].ToString() != ""))
                {
                    _groups.gid = set.Tables[0].Rows[0]["gid"].ToString();
                }
                return _groups;
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM wx_Userinfo ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public string GetTitle(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select top 1 title from  wx_UserGroup");
            builder.Append(" where id=" + id);
            string str = Convert.ToString(DbHelperSQL.GetSingle(builder.ToString()));
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            return str;
        }

        public bool Update(MxWeiXinPF.Model.wx_userinfo_BaseData model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update wx_Userinfo set ");
            builder.Append("wid=@wid,");
            builder.Append("wx_openid=@wx_openid,");
            builder.Append("requestType=@requestType,");
            builder.Append("requestContent=@requestContent,");
            builder.Append("responseType=@responseType,");
            builder.Append("reponseContent=@reponseContent,");
            builder.Append("createTime=@createTime,");
            builder.Append("wx_xmlContent=@wx_xmlContent,");
            builder.Append("createDate=@createDate,");
            builder.Append("flag=@flag,");
            builder.Append("rType=@rType,");
            builder.Append("remark=@remark,");
            builder.Append("extInt=@extInt,");
            builder.Append("extStr=@extStr,");
            builder.Append("extStr2=@extStr2,");
            builder.Append("extStr3=@extStr3");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@gid", SqlDbType.VarChar, 100), new SqlParameter("@openid", SqlDbType.VarChar, 100), new SqlParameter("@subscribe", SqlDbType.Int, 4), new SqlParameter("@nickname", SqlDbType.VarChar, 50), new SqlParameter("@sex", SqlDbType.Int, 4), new SqlParameter("@language", SqlDbType.VarChar, 50), new SqlParameter("@city", SqlDbType.NVarChar, 20), new SqlParameter("@province", SqlDbType.NVarChar, 20), new SqlParameter("@country", SqlDbType.NVarChar, 50), new SqlParameter("@headimgul", SqlDbType.VarChar, 500), new SqlParameter("@subscribe_time", SqlDbType.DateTime), new SqlParameter("@unsubscribe_time", SqlDbType.DateTime), new SqlParameter("@remark", SqlDbType.VarChar, 500), new SqlParameter("@userid", SqlDbType.Int, 4), new SqlParameter("@groupid", SqlDbType.Int, 4), 
                new SqlParameter("@groupname", SqlDbType.NVarChar, 100), new SqlParameter("@unionid", SqlDbType.VarChar, 100), new SqlParameter("@parentopenid", SqlDbType.VarChar, 100), new SqlParameter("@classname", SqlDbType.VarChar, 50), new SqlParameter("@subopenid", SqlDbType.VarChar, 500), new SqlParameter("@extInt", SqlDbType.Int, 4), new SqlParameter("@extStr1", SqlDbType.VarChar, 500), new SqlParameter("@extStr2", SqlDbType.VarChar, 100), new SqlParameter("@extStr3", SqlDbType.VarChar, 100), new SqlParameter("@id", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.wid;
            cmdParms[1].Value = model.gid;
            cmdParms[2].Value = model.openid;
            cmdParms[3].Value = model.subscribe;
            cmdParms[4].Value = model.nickname;
            cmdParms[5].Value = model.sex;
            cmdParms[6].Value = model.language;
            cmdParms[7].Value = model.city;
            cmdParms[8].Value = model.province;
            cmdParms[9].Value = model.country;
            cmdParms[10].Value = model.headimgul;
            cmdParms[11].Value = model.subscribe_time;
            cmdParms[12].Value = model.unsubscribe_time;
            cmdParms[13].Value = model.remark;
            cmdParms[14].Value = model.userid;
            cmdParms[15].Value = model.groupid;
            cmdParms[0x10].Value = model.groupname;
            cmdParms[0x11].Value = model.unionid;
            cmdParms[0x12].Value = model.parentopenid;
            cmdParms[0x13].Value = model.subopenid;
            cmdParms[20].Value = model.extInt;
            cmdParms[0x15].Value = model.extStr2;
            cmdParms[0x16].Value = model.extStr3;
            cmdParms[0x10].Value = model.id;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}
